home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_200 / 299_01 / plot.c < prev    next >
Text File  |  1989-12-28  |  10KB  |  301 lines

  1. /****************************************************************************/
  2. /* plot.c                                                                   */
  3. /*  (c) by Ronald Michaels. This program may be freely copied, modified,    */
  4. /*  transmitted, or used for any non-commercial purpose.                    */
  5. /* this file contains the functions necessary                               */
  6. /* to display the bp.c error graph                                          */ 
  7. /****************************************************************************/
  8.  
  9. #include <stdio.h>
  10. #include <fg.h>
  11. #include <stdlib.h>
  12. #include <dos.h>
  13.  
  14. #include"plot.h"
  15.  
  16. #define U(x) (unsigned int)(x)       /* type conversion  */
  17.  
  18. void build_grid(void);
  19. void prt_scr(void);
  20. void menu(int);
  21. void title(int);
  22. void printer_msg(int);
  23. int printer_status(void);
  24. void peek(unsigned,unsigned,void *,int);
  25.  
  26. int x_base = 59;
  27. int y_base = 23;
  28. int x_div = 60;
  29. int y_div = 30;
  30. static fg_line_t graph_line;   /* holds the coordinates for graph line */
  31.  
  32. /****************************************************************************/
  33. /* init_graph                                                               */
  34. /* this function initialises graphics and draws grid                        */
  35. /****************************************************************************/
  36. void init_graph()
  37. {
  38.     fg_pbox_t graph_box;  /* an array of 4 integers */
  39.     fg_line_t grid_line; /* array to hold line coordinates */
  40.  
  41.     int x_coord;
  42.     int y_coord;
  43.  
  44.    int i,         /* X-axis counter */
  45.        j;         /* Y-axis counter */
  46.  
  47.     if(fg_init_herc()==FG_NULL){
  48.         fputs("UNABLE TO OPEN GRAPHICS DEVICE\n",stderr);
  49.         exit (1);
  50.     }
  51.  
  52.     title(FG_WHITE);
  53.  
  54.     graph_box[FG_X1] = x_base;
  55.     graph_box[FG_Y1] = y_base;
  56.     graph_box[FG_X2] = x_base + 10 * x_div;
  57.     graph_box[FG_Y2] = y_base + 10 * y_div;
  58.  
  59.     fg_drawbox(FG_WHITE,FG_MODE_SET,~0,FG_LINE_SOLID,
  60.         graph_box,fg_displaybox);
  61.     
  62.    /* put in intersections */
  63.    for(i=0;i<11;i++){                       /* counts along x axis */
  64.       for(j=0;j<11;j++){                    /* counts along y axis */
  65.             x_coord = x_base + x_div * i;
  66.             y_coord = y_base + y_div * j;
  67.             grid_line[FG_X1] = x_coord - 2;
  68.             grid_line[FG_X2] = x_coord + 2;
  69.             grid_line[FG_Y1] = y_coord ;
  70.             grid_line[FG_Y2] = y_coord ;
  71.             fg_drawline(FG_WHITE,FG_MODE_SET,~0,FG_LINE_SOLID,grid_line);
  72.             grid_line[FG_X1] = x_coord ;
  73.             grid_line[FG_X2] = x_coord ;
  74.             grid_line[FG_Y1] = y_coord - 2;
  75.             grid_line[FG_Y2] = y_coord + 2;
  76.             fg_drawline(FG_WHITE,FG_MODE_SET,~0,FG_LINE_SOLID,grid_line);
  77.       }
  78.    }
  79.     fg_puts(FG_WHITE,FG_MODE_SET,~0,FG_ROT90,20,150,
  80.         "error",fg_displaybox);
  81.     fg_puts(FG_WHITE,FG_MODE_SET,~0,FG_ROT0,5,5,
  82.         "iter",fg_displaybox);
  83. }
  84.  
  85. /****************************************************************************/
  86. /* title                                                                    */
  87. /* this function prints title of graph                                      */
  88. /****************************************************************************/
  89. void title(
  90.     int color
  91.     )
  92. {
  93.     fg_puts(color,FG_MODE_SET,~0,FG_ROT0,x_base+10,y_base+10*y_div+10,
  94.         "Back Propagation Generalised Delta Rule Learning Program",
  95.         fg_displaybox);
  96. }
  97.  
  98.  
  99. /****************************************************************************/
  100. /* close_graph                                                              */
  101. /* this function clears the screen and resets to text                       */
  102. /****************************************************************************/
  103.  
  104. void close_graph()
  105. {
  106.    int choice;           /* program control choice */
  107.  
  108.    printf("%c",7);       /* sound bell */
  109.  
  110.    title(FG_BLACK);
  111.  
  112.    for(;;){
  113.       menu(FG_WHITE);
  114.       choice = getch();
  115.  
  116.       switch(choice){
  117.          case 'p':
  118.          case 'P':
  119.             /* print screen */
  120.             /* replace title */
  121.             menu(FG_BLACK);
  122.             printer_msg(FG_WHITE);
  123.             getch();
  124.             printer_msg(FG_BLACK);
  125.  
  126.             title(FG_WHITE);
  127.             prt_scr();      
  128.             title(FG_BLACK);
  129.  
  130.             break;
  131.          case 'v':
  132.          case 'V':
  133.              /* replace title */
  134.              menu(FG_BLACK);
  135.              title(FG_WHITE);
  136.              getch();
  137.              title(FG_BLACK);
  138.              break;
  139.          case 'q':
  140.          case 'Q':
  141.                 fg_term();
  142.             return;
  143.          default:
  144.             break;
  145.       }
  146.    }
  147. }
  148.  
  149.  
  150. /****************************************************************************/
  151. /* set_scales                                                               */
  152. /* this function takes the maximum values for X and Y and scales the axes   */
  153. /****************************************************************************/
  154.  
  155. void set_scales(
  156.    double error,
  157.    int iter,
  158.    double *x_scale,
  159.    double *y_scale
  160. )
  161. {
  162.  
  163.  
  164.    double y_max, y_step, y_val;
  165.    double x_max, x_step, x_val;
  166.  
  167.    char buff[5];     /* holds the string conversion of scale values  */
  168.  
  169.    int i,         /* X-axis counter */
  170.        j;         /* Y-axis counter */
  171.  
  172.    /* find the smallest integer value greater than error */
  173.    for(y_max=0.0;y_max<error;y_max++);
  174.    *y_scale = y_max;
  175.  
  176.    y_step = y_max/10.0;
  177.  
  178.    for(j=0;j<11;j++){
  179.       y_val = ((double)j)*y_step;
  180.       sprintf(buff,"%03.1f",y_val);
  181.        fg_puts(FG_WHITE,FG_MODE_SET,~0,FG_ROT0,25,y_base+y_div*j-5,
  182.             buff,fg_displaybox);
  183.    }
  184.  
  185.    /* find the smallest round hundreds value greater than iter */
  186.    for(x_max=0.0;x_max<((double)iter);x_max+=100.0);
  187.    *x_scale = x_max;
  188.    x_step = x_max/10.0;
  189.  
  190.    for(i=0;i<11;i++){
  191.       x_val = ((double)i)*x_step;
  192.       sprintf(buff,"%3.0f",x_val);
  193.        fg_puts(FG_WHITE,FG_MODE_SET,~0,FG_ROT0,x_base+x_div*i-15,5,
  194.             buff,fg_displaybox);
  195.    }
  196.     graph_line[FG_X1] = x_base;
  197.     graph_line[FG_Y1] = (int)((error/y_max)*(double)(10*y_div))+y_base;
  198. }
  199.  
  200. /****************************************************************************/
  201. /* point                                                                    */
  202. /* this function plots a line from previous value to new value              */
  203. /****************************************************************************/
  204.  
  205. void point(
  206.    double error,
  207.    int iter,
  208.    double x_max,
  209.    double y_max
  210. )
  211. {
  212.     graph_line[FG_X2] = (int)(((double)iter/x_max)*(double)(10*x_div))+x_base;
  213.     graph_line[FG_Y2] = (int)((error/y_max)*(double)(10*y_div))+y_base;
  214.  
  215.     fg_drawline(FG_WHITE,FG_MODE_SET,~0,FG_LINE_SOLID,graph_line);
  216.  
  217.     graph_line[FG_X1] = graph_line[FG_X2];
  218.     graph_line[FG_Y1] = graph_line[FG_Y2];
  219. }
  220.  
  221. /****************************************************************************/
  222. /* menu                                                                     */
  223. /* this function displays menu                                              */
  224. /****************************************************************************/
  225. void menu(
  226.    int color                      /* color of letters */
  227. )
  228. {
  229.     fg_puts(color,FG_MODE_SET,~0,FG_ROT0,x_base+10,y_base+10*y_div+10,
  230.         "View    Print    Quit",
  231.         fg_displaybox);
  232. }
  233. /****************************************************************************/
  234. /* printer_msg                                                              */
  235. /* this function prints a printer warning                                   */
  236. /****************************************************************************/
  237. void printer_msg(
  238.    int color
  239. )
  240. {
  241.     fg_puts(color,FG_MODE_SET,~0,FG_ROT0,x_base+10,y_base+10*y_div+10,
  242.         "CHECK PRINTER     press any key when ready",
  243.         fg_displaybox);
  244.  
  245. }
  246. /****************************************************************************/
  247. /* prt_scr                                                                  */
  248. /* this function prints out video memory to a Star printer                  */
  249. /* if you want to print 8 or 9 pin graphics you will need to replace fputc  */
  250. /* with a function that does not convert \n to \r\n                         */
  251. /***********************************************************